package service.impl;

import bean.*;

import mapper.IRoomMapper;
import mapper.IStudentMapper;
import org.apache.ibatis.session.SqlSession;
import service.IRoomService;

import java.util.List;

public class RoomServiceImpl extends BaseService implements IRoomService {

    @Override
    public CutPageBean<RoomBean> listRoomByPage(int pageNo, String roomAddr, String status, String liveRoom, String type) {
        CutPageBean<RoomBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        cutBean.setList(mapper.cutByItemList((pageNo - 1) * CutPageBean.PAGESIZE,
                CutPageBean.PAGESIZE, roomAddr, status, liveRoom, type));
        cutBean.setCount(mapper.cutByItemCount(roomAddr, status, liveRoom, type));
        session.close();
        return cutBean;
    }

    @Override
    public boolean add(RoomBean room) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        mapper.add(room);
        session.commit();
        session.close();
        return true;
    }

    @Override
    public boolean deleteRoom(int roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        Integer studentNum = mapper.getStudentCount(roomId);
        if (studentNum != 0) {
            return false;
        }
        mapper.deleteSet(roomId);
        mapper.deleteRoom(roomId);
        session.commit();
        session.close();
        return true;
    }

    @Override
    public List<StudentBean> findById(int roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<StudentBean> list = mapper.findById(roomId);
        session.close();
        return list;
    }

    @Override
    public List<RoomBean> listRoomByType(String type) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<RoomBean> list = mapper.listRoomByType(type);
        session.close();
        return list;
    }
}
